package cn.itsource.pethome.org.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.itsource.pethome.base.domain.BaseQuery;
import cn.itsource.pethome.base.domain.PageResult;
import cn.itsource.pethome.base.util.EmailUtil;
import cn.itsource.pethome.base.util.RedisService;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.domain.ShopDTO;
import cn.itsource.pethome.org.mapper.EmployeeMapper;
import cn.itsource.pethome.org.mapper.ShopMapper;
import cn.itsource.pethome.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    RedisService redisService;
    @Autowired
    EmailUtil emailUtil;

    @Override
    public void insert(Shop shop) {

    }

    @Override
    public void update(Shop shop) {

    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public void deleteBatch(List<Long> ids) throws Exception {

    }

    @Override
    public PageResult findPage(BaseQuery query) {
        return null;
    }

    @Override
    public List<Shop> findAll() {
        return shopMapper.selectAll();
    }

    @Override
    public Shop findOne(Long id) {
        return null;
    }

    /**
     * 我们店铺注册的方法
     * @param shopDTO
     */
    @Override
    public void register(ShopDTO shopDTO) {
        //1.我们首先要参数校验
        //2.新增到t_shop 店铺表
        //2.1新增到店铺时,需要校验店铺名称是否重复
        if (shopMapper.findByName(shopDTO.getShop().getName())){

            throw new RuntimeException("店铺名已注册");
        }
        //2.2新增店铺
        shopMapper.insertSelective(shopDTO.getShop());

        //3.t_employee 员工表
        //3.1补充需要新增的员工字段 shopId,ShopAdmin
        shopDTO.getEmployee().setShopAdmin(Boolean.TRUE);
        shopDTO.getEmployee().setShopId(shopDTO.getShop().getId());
        //这里就是加盐
        shopDTO.getEmployee().createSalt();
        //这里就是给密码加密
        shopDTO.getEmployee().passwordMD5();

        employeeMapper.insertSelective(shopDTO.getEmployee());
        //需要发送邮箱到管理员邮箱中
        //1.创建一个该员工的唯一标识
        //我们这里就使用uuid
        String uuid = IdUtil.fastSimpleUUID();
        //邮箱发送需要有限制时间,这里我们使用redis,key是 uuid ,value 是邮箱,设置过期时间十分钟
        redisService.setStringKeyAndValue(uuid,shopDTO.getEmployee().getEmail(),10);
        //我们现在就要给用户发送邮件认证了,给他一个我们服务器的认证地址
        emailUtil.send("297510323@qq.com","宠物乐园店铺入驻激活","尊敬的用户您好:请在10分钟内点击该地址完成激活操作"+"http://localhost/shop/empl/state/"+uuid);
    }

    @Override
    public String emplState(String uuid) {
        //0.首先要判断key是否存在
        Boolean aBoolean = redisService.hasKey(uuid);
        if (!aBoolean){
           return "尊敬的用户,您的验证时间已到期,请重新发送";
        }
        //1.从redis获取数据,取出用户的邮箱
        String email = redisService.getKeyObjectValue(uuid, String.class);
        //2.获取到用户邮箱,开始做激活操作,t_employee t_shop
        //2.1激活该用户的状态state
        Employee employee = employeeMapper.findByEmail(email);
        //这里还是做一布判断用户是否存在
        if (Objects.isNull(employee)){
            return "邮箱不存在";
        }
        employee.setState(Boolean.TRUE);
        //2.2激活店铺的状态
        Long shopId = employee.getShopId();
        Shop shop = new Shop();
        shop.setId(shopId);
        shop.setState(3);
        shopMapper.updateByPrimaryKeySelective(shop);
        return "激活成功";
    }


}
